Rem Build Version: 3.0.104.0
@cls
@echo off

REM  
REM  PrepImage.bat - Used to create an ADS image for an unattened OS install
REM  
REM  
REM      THIS CODE AND INFORMATION IS PROVIDED TO YOU FOR YOUR REFERENTIAL 
REM      PURPOSES ONLY, AND IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY REM  KIND, 
REM      EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
REM      WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE, 
REM      AND MAY NOT BE REDISTRIBUTED IN ANY MANNER.
REM  
REM      Copyright (c) Microsoft Corporation. All rights reserved.
REM  
REM
REM  
REM  Unattended install using ADS, How it works:
REM  
REM  	1.  Prepare an ADS image of windows install files (i386), including bootstrap files
REM  	2.  Deploy this image to a new system using an ADS sequence
REM  	3.  Personalize the image (computername, agent parameters, etc)
REM  	4.  Reboot the device, unattended install is started with a personalized answer file
REM  	5.  cmdlines.txt file runs and installs the ADS agent
REM  	6.  On the final reboot the system comes back up and communicates to the controller
REM  	7.  Additional actions can be performed via the ADS deploy sequence.
REM  
REM
REM  Prerequisites:	
REM  	1.  This procedure runs on an ADS controller (dependencies on ADS utilities)
REM  	2.  Empty 500mb volume as a drive letter, ( example fREM )
REM  	3.  i386 directory from windows 2003 cd ( example c:\w2k3\i386 )
REM  	4.  Customized unattended install answer file ( example c:\unattend.txt)
REM  	5.  Valid windows product key (edit unattend.txt)
REM  	6.  Edit environment variables in PrepImage.bat for your envionment
REM
REM
REM  Unattended install using ADS, Step-by-Step:
REM	1.  Create a 500mb parition on your ADS controller and assign it a drive letter
REM	2.  Format the new drive it with a no volume name
REM	3.  Copy the sample files for OS Unattened install to c:\ of your controller
REM	4.  Edit the productkey value in the unattend.txt with a valid product key
REM	5.  Edit the environment varible section of PrepImage.bat to match your configuration
REM	6.  Run Prepimage.bat to create the unattened install image
REM	7.  Prepare a new server (device) by booting up the the DA (you can do more than one machine)
REM	8.  Create an ADS device varible for each device called Computername and give it a valid computername value
REM	9.  Run the Job w2k3unattend on the device, (or you can target a set of devices)
REM	10.  The process should complete in 30-40 min.
REM	11.  Customize w2k3unattenc.xml with additional post-os configuration tasks as needed.
REM
REM
REM  Dependencies:
REM  	1.  unattend.txt answerfile
REM  	2.  w2k3unattend.xml (sequence to deploy the unattened install image)
REM  	3.  This file, PrepImage.bat
REM  	
REM
REM
REM 
REM  

REM  Set variables to match your configuration
REM  
REM  ******************************************************************************************
REM  

set targetdrive=f:
set path_to_answerfile=c:\unattend.txt
set w2k3source=c:\w2003.ee\i386
set adsagentsource=c:\ads
set ADSinstallpath=c:\Program Files\Microsoft ADS
set imagename=w2k3_Unattended.img
set imagepath=c:\
set ADSjobname=c:\w2k3unattend.xml

REM
REM note if you use a different imagename be sure to update the deploy sequence
REM


REM  
REM  ******************************************************************************************
REM  

REM  
REM  echo environment  variables for verification
REM  

Echo.
Echo Prepimage.bat 
echo.
echo This procedure will prepare an ADS image file with windows setup files as a windows unatteneded setup
echo.
echo See comments in PrepImage.bat for additional detail
echo.
echo.
echo.
echo Environment Variables:
Echo.
Echo.
Echo Targetdrive=%targetdrive%
Echo Path_to_Answerfile=%path_to_answerfile%
Echo W2k3Source=%w2k3source%
Echo AdsAgentSource=%adsagentsource%
Echo ADSInstallPath=%ADSinstallpath%
Echo ImageName=%imagename%
Echo ImagePath=%imagepath%
Echo ADSJObName=%adsjobname%
Echo.
Echo.
Echo Review variable settings, hit ctrl+c to cancel
echo.
echo.
echo.
echo.
echo REMINDER:  Have you configured a ProductID in your %path_to_answerfile% ?
echo.
echo.
pause

REM  clear the temp drive of all files by formatting

echo.
echo Format %targetdrive%
echo.

Format %targetdrive% /q 
echo errorlevel=%errorlevel%
goto return%errorlevel% 
:return1
echo.
Echo Error in Format %targetdrive%
echo.
pause
goto errorend
:return5
echo.
Echo No on format selected
cho.
goto errorend
:return0
Echo Success

REM  delete the current version of the image if it exists

echo.
echo clean up previous image
echo.

Adsimage /delete "%imagename%" /force
cls

REM  pre-populate the tempdrive with windows setup files including bootstrap files
REM  check windows path for setup files

if not exist "%w2k3source%\winnt32.exe" Echo.& echo Error: %w2k3source%\winnt32.exe not found & echo. & goto errorend

REM check for unattend.txt file

if not exist "%path_to_answerfile%" Echo.& echo Error: %path_to_answerfile% not found & echo. & goto errorend

echo.
echo prepare temp partition with windows source files
echo.

"%w2k3source%\winnt32.exe" /s:"%w2k3source%" /noreboot /syspart:%targetdrive% /tempdrive:%targetdrive% /unattend:"%path_to_answerfile%"

echo errorlevel=%errorlevel%

REM  NOTE:  winn32 with the /syspart option may leave the temporary partition marked as active and may be the default boot device
REM         use diskpart to reset the active partitions.
 
echo.
echo reset active disks using diskpart
echo.
echo select volume %targetdrive% >c:\dpscript.txt
echo active >>c:\dpscript.txt
echo rescan >>c:\dpscript.txt
echo select volume c: >>c:\dpscript.txt
echo active >>c:\dpscript.txt
echo rescan >>c:\dpscript.txt
echo.
diskpart /s c:\dpscript.txt
echo.



goto installerror%errorlevel% 
:installerror1
echo.
Echo Error in windows setup 
echo.

goto errorend

:installerror0
Echo Success


cls
REM  rename files for volume size, only needed on an upgrade
REM  rename %targetdrive%\$win_nt$.~bt\migrate.inf %targetdrive%\$win_nt$.~bt\omigrate.inf


REM  copy ADS agent files to build source so that ADS agent can be installed during cmdlines.txt processing
REM  check windows path for setup files

if not exist "%adsinstallpath%\certificate\adsroot.cer" Echo.& echo Error: %adsinstallpath%\certificate\adsroot.cer not found & echo. & goto errorend
if not exist "%adsagentsource%\adsagentsetup.msi" Echo.& echo Error: %adsagentsource%\adsagentsetup.msi not found & echo. & goto errorend

echo.
echo Copy ADS agent files to temp partition %targetdrive%
echo.
mkdir %targetdrive%\$win_Nt$.~LS\$Oem$\$1\localinstalltemp
copy "%adsinstallpath%\certificate\adsroot.cer" %targetdrive%\$win_Nt$.~LS\$Oem$\$1\localinstalltemp      
copy "%adsagentsource%\adsagentsetup.msi" %targetdrive%\$win_Nt$.~LS\$Oem$\$1\localinstalltemp

REM  create cmdlines.txt command for agent install during GUI mode setup
REM  Values between ^carrot^ symbols will be replaced during processing of ADS install sequence with proper values

echo. 
echo Create cmdlines.txt file
echo.
echo [commands] > %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "MSIEXEC.exe /i c:\localinstalltemp\AdsAgentSetup.msi /qb CertPath=c:\localinstalltemp\adsroot.cer Userlogon=0" >> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "reg add HKLM\SYSTEM\ControlSet001\Control\BMSS /v controlleripaddress /t REG_MULTI_SZ /d ^ADS_Controller_IP^ /f">> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "reg add HKLM\SYSTEM\ControlSet001\Control\BMSS /v bmdpipport /t REG_DWORD /d ^bmdpipport^ /f">> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "reg add HKLM\SYSTEM\ControlSet001\Control\BMSS /v bmcpipport /t REG_DWORD /d ^bmcpipport^ /f">> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "reg add HKLM\SYSTEM\ControlSet001\Control\BMSS /v bindexcept /t REG_MULTI_SZ /d ^bindexcept^ /f">> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "reg add HKLM\SYSTEM\ControlSet001\Control\BMSS /v bindpolicy /t REG_DWORD /d 0 /f">> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt
echo "cmd /c rmdir /s /q c:\localinstaltemp" >> %targetdrive%\$win_Nt$.~LS\$Oem$\cmdlines.txt


REM  create ADS image of volume using ADS imgdeploy utility

echo.
echo Capture temporary partition %targetdrive%
echo.

Imgdeploy /c /y %targetdrive% "%imagepath%%imagename%" "W2k3 Unattended Install Image"

REM  load the image to the ADS controller using ADSimage

echo.
echo add the image %imagename% to the ADS controller
echo.

adsimage /add "%imagename%" /path "%imagepath%%imagename%" /description "w2k3 Unattended install image"

REM clear out the jobtempalte and re-add

if not exist "%adsjobname%" Echo.& echo Error: %adsjobname%  not found & echo. & goto errorend

echo.
echo Add the job %adsjobname% to the ADS controller
echo.

adsjobtemplate /d "w2k3 Unattend"
adsjobtemplate /c "W2k3 Unattend" /description "Deployes an OS via unattened install" /path "%ADSjobname%"


echo.
echo All done...
echo.
echo.
echo.
echo.
echo Hint;
echo.
echo Now test your unattened install by running the w2k3unattend job on a new device
echo.
echo Make sure to create the device variable COMPUTERNAME for each device that will run the w2k3unattend job
echo.
echo.
echo.
echo.


goto veryend





:errorend
Echo.
Echo.
echo.
echo.
echo An Error Occured  Please check all variables
echo.
echo.
echo.
echo.
echo.

:veryend

REM  PrepImage.bat
